//
//  DuiHuanOrderController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/10.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "DuiHuanOrderController.h"
#import "DuiHuanOrderView.h"
#import "YXLabelView.h"
#import "JiFenNewsModel.h"

@interface DuiHuanOrderController ()<UITextViewDelegate,YXLabelViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *BackView;
@property (weak, nonatomic) IBOutlet UIScrollView *DHScrollView;
@property (strong, nonatomic) DuiHuanOrderView * DHView;
@property (strong, nonatomic) NSMutableArray * TitleArr;
@property (strong, nonatomic) NSString * ShopID;

@end

@implementation DuiHuanOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"兑换报单";
    self.BackView.hidden = YES;
    self.DHView = [[NSBundle mainBundle] loadNibNamed:@"DuiHuanOrderView" owner:self options:nil][0];
    [self.DHScrollView addSubview:self.DHView];
    
    for (int i = 0; i < self.TypeArr.count; i ++) {
        JiFenNewsModel * model = self.TypeArr[i];
        [self.TitleArr addObject:model.goods_name];
    }
    
    YXLabelView * labelV = [[YXLabelView alloc] initWithFrame:self.DHView.DHTypeView.frame dataArr:self.TitleArr selectBtn:self.ChooseID];
    labelV.delegate = self;
    
    [self.DHView.DHTypeView addSubview:labelV];
    self.DHView.DHTypeViewH.constant = labelV.bounds.size.height;
    JiFenNewsModel * model = self.TypeArr[self.ChooseID];
    NSLog(@"ID = %@ Name = %@",model.goods_id,model.goods_name);
    self.ShopID = [NSString stringWithFormat:@"%@",model.goods_id];
    
    self.DHScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.DHView.frame));
    if ([self.ShowType isEqual:@"0"]) {
        self.DHView.DHCardView.hidden = YES;
        self.DHView.DHCardViewH.constant = 0;
    }else{
        self.DHView.DHCardView.hidden = NO;
        self.DHView.DHCardViewH.constant = 60;
    }
    self.DHView.DHCardImage.userInteractionEnabled = YES;
    UITapGestureRecognizer * chooseImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];
    [self.DHView.DHCardImage addGestureRecognizer:chooseImage];
    
    self.DHView.Alterlabel.hidden = NO;
    self.DHView.TextView.delegate = self;
    self.DHView.TextView.layer.borderWidth = 1;
    self.DHView.TextView.layer.borderColor = KRGBColor(218, 218, 218).CGColor;
    
    self.DHView.BZText.layer.borderWidth = 1;
    self.DHView.BZText.layer.borderColor = KRGBColor(218, 218, 218).CGColor;
    
    self.DHView.UpLoadBtn.layer.cornerRadius = 8;
    self.DHView.UpLoadBtn.layer.masksToBounds = YES;
    
    [self.DHView.UpLoadBtn addTarget:self action:@selector(ClickOnBtnToUpLoadDuiHuanOrder:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)ClickOnLabelOfNumb:(NSInteger)index andTypr:(BOOL)selentBool;{
    JiFenNewsModel * model = self.TypeArr[index-100];
    NSLog(@"ID = %@ Name = %@",model.goods_id,model.goods_name);
    self.ShopID = [NSString stringWithFormat:@"%@",model.goods_id];
}

- (void)chooseImage{
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    imagePickerVc.allowPickingVideo = NO;//隐藏视频
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        if (photos.count > 0) {
            self.DHView.DHCardImage.image = photos[0];
        }
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.DHView.Alterlabel.hidden = YES;
}


- (void)textViewDidEndEditing:(UITextView *)textView{
    if (self.DHView.TextView.text.length == 0) {
        self.DHView.Alterlabel.hidden = NO;
    }else{
        self.DHView.Alterlabel.hidden = YES;
    }
}

- (void)ClickOnBtnToUpLoadDuiHuanOrder:(UIButton *)sender {
    [self.view endEditing:YES];
    
    if ([self.ShowType isEqual:@"0"]) {
        [self UpLoadDuiHuanOrderNonePic];
    }else{
        [self UpLoadDuiHuanOrderWithPic];
    }
    
    
}


- (void)UpLoadDuiHuanOrderWithPic {
    if (self.ShopID.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择产品类型"];
        return;
    }
    
    
    if (self.DHView.DHCardImage.image == nil) {
        [MBProgressHUD showAlertViewWithText:@"请选择上传的图片"];
        return;
    }
    
    if (self.DHView.TextView.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写兑换码"];
        return;
    }
    NSLog(@"=======%@",self.DHView.DHCardImage.image);
    NSMutableArray * ImageData = [[NSMutableArray alloc] init];
    [ImageData addObject:self.DHView.DHCardImage.image];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    
    [MainObjectWay PostWithMethod:@"upload" andAction:@"Server" andAddParam:NO parameters:dic Name:@"file" formData:ImageData success:^(id responseObject) {
        NSLog(@"上传成功信息 = %@",responseObject);
        NSLog(@"消息 = %@",responseObject[@"msg"]);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSString * ImageStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"link"]];
            NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
            [dic setObject:KUserID forKey:@"order_member"];
            [dic setObject:self.ShopID forKey:@"order_goods"];
            [dic setObject:ImageStr forKey:@"order_pic"];
            [dic setObject:self.DHView.TextView.text forKey:@"order_content"];
            if (self.DHView.BZText.text.length == 0) {
                [dic setObject:@"" forKey:@"order_bak"];
            }else{
                [dic setObject:self.DHView.BZText.text forKey:@"order_bak"];
            }
            
            [MainObjectWay PostWithMethod:@"submit_order" andAction:@"Points" andAddParam:YES parameters:dic success:^(id responseObject) {
                NSLog(@"提交结果信息 = %@",responseObject);
                if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                    [MBProgressHUD showAlertViewWithText:@"提交成功"];
                    self.BackView.hidden = NO;
                }
            } failure:^(NSError *error) {
                NSLog(@"错误信息 = %@",error);
            }];
        }
    } failure:^(NSError *error) {
        NSLog(@"上传失败信息 = %@",error);
    }];
    
}

- (void)UpLoadDuiHuanOrderNonePic {
    if (self.ShopID.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择产品类型"];
        return;
    }
    
    
    if (self.DHView.TextView.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写兑换码"];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUserID forKey:@"order_member"];
    [dic setObject:self.ShopID forKey:@"order_goods"];
    [dic setObject:@"" forKey:@"order_pic"];
    [dic setObject:self.DHView.TextView.text forKey:@"order_content"];
    if (self.DHView.BZText.text.length == 0) {
        [dic setObject:@"" forKey:@"order_bak"];
    }else{
        [dic setObject:self.DHView.BZText.text forKey:@"order_bak"];
    }
    
    [MainObjectWay PostWithMethod:@"submit_order" andAction:@"Points" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"提交结果信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [MBProgressHUD showAlertViewWithText:@"提交成功"];
            self.BackView.hidden = NO;
        }
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
    
    
}
- (IBAction)ClickOnBottomBtnOfDuiHuanOrderController:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (NSMutableArray *)TitleArr{
    if (!_TitleArr) {
        _TitleArr = [NSMutableArray array];
    }
    return _TitleArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
